home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Collection of Tools & Utilities
/
Collection of Tools and Utilities.iso
/
pascal
/
mystic.zip
/
PAS6.DOC
< prev
next >
Wrap
Text File
|
1986-02-27
|
16KB
|
914 lines
Mystic Pascal User Manual 59
9. Input/Output
Mystic Pascal supports all standard Pascal I/O procedures
and functions and has extensions to support random disk file
access and to assign a DOS file name to a Pascal file variable.
+ ASSIGN associate a file name with a file variable
+ CLOSE terminate processing on a file
EOF Boolean function indicates end-of-file
condition
EOLN Boolean function indicates end-of-line
condition for text files
1.6 GET move file pointer to next component
+ IORESULT integer function status of I/O operation
PAGE advance textfile output to new page
1.6 PUT append buffer variable to file
READ obtain input from console or disk file
READLN obtain textfile input from new line
RESET prepare an existing file for input
REWRITE create a file for output
+ SEEK position the file pointer for random access
WRITE output data to console, printer, disk file
WRITELN output textfile data and terminate line
Section 9: Input/Output
Mystic Pascal User Manual 60
9.1 File Variables
File variables, like other variables, are declared in a VAR
section. They indicated the data type of the file's components.
TYPE
DATAFILE = FILE OF REAL;
VAR
F1 : DATAFILE;
F2 : FILE OF ARRAY [0..63] OF CHAR;
F3 : TEXT;
Textfiles are declared by the predefined type TEXT.
Textfiles consist of lines of characters separated by the end-of-
line byte sequence 0DH, 0AH (carriage return, line feed).
9.2 Standard Files
The DOS operating system supports several standard "files"
or logical devices which are always available. These may be used
directly as file variables in Read/ln and Write/ln. They may
also be assigned to Pascal file variables by the Assign
procedure.
CON standard console device (input/output)
KBD keyboard device (input without echoing)
AUX auxiliary device (input/output)
LST printer (output)
PRN printer (output)
If no file variable is specified as the first parameter in
the Read/ln or Write/ln procedures, the default files INPUT and
OUTPUT respectively are used. These are assigned to the standard
console and keyboard devices.
9.3 How to Send Data to Your Printer
To route output to your printer specify the Standard File
LST or PRN in your WRITE or WRITELN statements.
WRITELN( PRN, 'This line will be printed.');
WRITE( PRN, 'X =',X,' Y =',Y,' Z =',Z );
Section 9: Input/Output
Mystic Pascal User Manual 61
9.4 ASSIGN (Non-Standard Feature)
ASSIGN( Filvar, String )
ASSIGN( Filvar, Standard_file )
The procedure Assign is used to associate a file variable
with a particular disk file or standard file.
The ability to assign standard files to a file variable
allows complete I/O redirection within a Pascal program. For
example, a program which creates reports could route its output
to a disk file one time and to a printer or console another time.
There is no need for separate WRITE statements for each type of
output.
10: WRITELN('Select output device for Report Listing');
WRITELN('1 = Console 2 = Printer 3 = Diskfile');
READLN(X);
CASE X OF
1 : ASSIGN( REPORTFILE, CON );
2 : ASSIGN( REPORTFILE, PRN );
3 : ASSIGN( REPORTFILE, 'REPORT.LST' );
ELSE GOTO 10
END;
Section 9: Input/Output
Mystic Pascal User Manual 62
9.5 CLOSE (Non-Standard Feature)
CLOSE( Filvar )
The Close procedure must be called after completing
processing on a disk file to ensure that the disk directory is
updated. Failure to Close a file after updating it may result in
the loss of data.
Closing a file variable to which a standard file has been
assigned is treated as a null operation.
CLOSE( F1 );
CLOSE( REPORTFILE );
CLOSE( SORTFILE2 );
Section 9: Input/Output
Mystic Pascal User Manual 63
9.6 EOF
EOF( Filvar )
Eof is a Boolean function which indicates the end-of-file
condition. Eof is true only when the file pointer points past
the last component of the file, otherwise it is false.
{ Copy F1 into F2 }
WHILE NOT EOF( F1 ) DO
BEGIN
READ( F1, X );
WRITE( F2, X )
END
Section 9: Input/Output
Mystic Pascal User Manual 64
9.7 EOLN
EOLN
EOLN( Filvar )
Eoln is a Boolean function which indicates the end-of-line
condition of a textfile. If no file variable is specified, the
function assumes the file INPUT. It is a compiler error if the
file variable is not a textfile.
{ Average N numbers from the console }
SUM := 0; N := 0;
WHILE NOT EOLN DO
BEGIN
READ( X );
SUM := SUM + X;
N := N + 1
END
WRITELN( 'The average is', SUM DIV N );
Section 9: Input/Output
Mystic Pascal User Manual 65
9.8 GET, PUT and Buffer Variables ** 1.6 **
Standard Pascal supports Input/Output operations which are
more primitive than Read and Write. In fact Read and Write are
defined in terms of the procedures Get and Put, which only move
data between a file and its buffer variable.
A buffer variable is associated with each file. Its data
type is the same as the components of the file. A buffer
variable is accessed by using the file variable's name followed
by an uparrow.
WRITE( F, X ) is equivalent to F^ := X; PUT( F )
READ( F, X ) is equivalent to GET( F ); X := F^
GET( Filvar )
The Get procedure advances the file pointer to the next
component of the file. The current component is available in the
file's buffer variable.
PUT( Filvar )
The Put procedure appends the value in the buffer variable
to the file. The condition EOF( Filvar ) must be true when Put
is called.